FROM php:7.4-fpm

# 安装系统依赖
RUN apt-get update && apt-get install -y \
    git \
    curl \
    libpng-dev \
    libonig-dev \
    libxml2-dev \
    libzip-dev \
    zip \
    unzip \
    nginx \
    redis-server \
    supervisor \
    cron

# 安装PHP扩展
RUN docker-php-ext-install \
    pdo_mysql \
    mysqli \
    mbstring \
    exif \
    pcntl \
    bcmath \
    gd \
    opcache \
    zip \
    sockets \
    calendar \
    soap \
    intl \
    fileinfo

# 安装额外的PHP扩展（Redis和igbinary）
RUN pecl install redis igbinary && \
    docker-php-ext-enable redis igbinary

# 确保shell_exec函数可用
RUN echo "disable_functions = " > /usr/local/etc/php/conf.d/docker-php-disable-functions.ini

# 为API项目配置Nginx
RUN mkdir -p /etc/nginx/sites-available /etc/nginx/sites-enabled
COPY ./api /var/www/api
RUN echo "server {\n\
    listen 9501;\n\
    server_name api.cn;\n\
    root /var/www/api/public;\n\
    index index.php;\n\
    location ~* (runtime|application)/{\n\
        return 403;\n\
    }\n\
    location / {\n\
        if (!-e \$request_filename){\n\
            rewrite  ^(.*)$  /index.php?s=\$1  last;\n\
        }\n\
    }\n\
    location ~ \.php$ {\n\
        fastcgi_pass 127.0.0.1:9000;\n\
        fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;\n\
        include fastcgi_params;\n\
    }\n\
}" > /etc/nginx/sites-available/api.conf

# 为Admin项目配置Nginx
COPY ./admin /var/www/admin
RUN echo "server {\n\
    listen 9501;\n\
    server_name admin.cn;\n\
    root /var/www/admin/public;\n\
    index index.php;\n\
    location ~* (runtime|application)/{\n\
        return 403;\n\
    }\n\
    location / {\n\
        if (!-e \$request_filename){\n\
            rewrite  ^(.*)$  /index.php?s=\$1  last;\n\
        }\n\
    }\n\
    location ~ \.php$ {\n\
        fastcgi_pass 127.0.0.1:9000;\n\
        fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;\n\
        include fastcgi_params;\n\
    }\n\
}" > /etc/nginx/sites-available/admin.conf

# 启用Nginx站点配置
RUN ln -s /etc/nginx/sites-available/api.conf /etc/nginx/sites-enabled/ && \
    ln -s /etc/nginx/sites-available/admin.conf /etc/nginx/sites-enabled/ && \
    rm -f /etc/nginx/sites-enabled/default

# 设置适当的文件权限
RUN chown -R www-data:www-data /var/www/api && \
    chown -R www-data:www-data /var/www/admin && \
    chmod -R 755 /var/www

# 配置supervisor来运行多个服务
RUN mkdir -p /var/log/supervisor
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf

# 复制启动脚本并设置执行权限
COPY docker-entrypoint.sh /docker-entrypoint.sh
RUN chmod +x /docker-entrypoint.sh

# 设置环境变量
ENV DB_HOST=mysql \
    DB_PORT=3306 \
    DB_NAME=learn \
    DB_USER=learn \
    DB_PASSWORD=123456 \
    ES_HOST=elasticsearch

# 暴露端口
EXPOSE 9501

# 设置工作目录
WORKDIR /var/www

# 启动服务
CMD ["/docker-entrypoint.sh"] 